package Hbase;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * @author 王至甲
 * @date 2022/3/11 21:04
 */
@WebServlet("/register")
public class RegisterTable extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","niit01");
        Connection connection = ConnectionFactory.createConnection(conf);

        Admin admin = connection.getAdmin();
        HTable table = new HTable(conf,"registertable");


        String rowkey = "3";
        Put put = new Put(Bytes.toBytes(rowkey));
        put.addColumn(Bytes.toBytes("cf1"),Bytes.toBytes("username"),Bytes.toBytes(username));
        put.addColumn(Bytes.toBytes("cf1"),Bytes.toBytes("password"),Bytes.toBytes(password));

        table.put(put);
        System.out.println("注册成功！");
//        PrintWriter printWriter = resp.getWriter();
//        resp.setContentType("text/html;charset=utf-8");
//        printWriter.write("sign up successfully！");

        //printWriter.write("<a href=\"index.jsp\">log in!</a>");
        req.getRequestDispatcher("/index.jsp").forward(req,resp);

    }

    public static void main(String[] args) {

    }
}
